package pers.sports.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.sports.service.EmailService;
import pers.sports.util.VerificationCodeSix;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("/email")
public class EmailController {

    private final EmailService emailService;


    public EmailController(EmailService emailService) {
        this.emailService = emailService;
    }

    @ResponseBody
    @RequestMapping(value = "/forgetPwd", method = RequestMethod.POST)
    public String forgetPwd(String email_text, HttpServletRequest request) {
        String six_code = VerificationCodeSix.VerificationCode();
        try {
            emailService.sendSimpleMail(email_text, "Sports系统验证码",
                    "您正在申请重置密码，如果不是您本人操作，请忽略本邮件。验证码 : " + six_code);
            request.getSession().setAttribute("email_code", six_code);
        } catch (Exception e) {
            return "failure";
        }
        return "success";
    }
}


